import express, { Router } from 'express';
import { EvaluationController } from '../controllers/evaluationController';
import { auth } from '../middleware/auth';
import { roleAuth } from '../middleware/roleAuth';
import { asyncHandler } from '../middleware/asyncHandler';
import { createHandler } from '../types/http';
import {
  validateEvaluation,
  validateCompleteReview,
  validateBatchDelete
} from '../middleware/validators/evaluationValidator';
import {
  EvaluationParams,
  EvaluationBody,
  EvaluationResponse,
  BatchDeleteBody,
  EvaluationQuery,
  EvaluationStatusUpdate,
  EvaluationUpdateRequest
} from '../types/evaluation';
import { EmptyParams } from '../types/http';
import { safeObjectId } from '../utils/requestUtils';

const router: Router = express.Router();

// 所有路由都需要认证
router.use(auth);

// 获取评估列表
router.get('/', createHandler<EmptyParams, EvaluationResponse[], void, EvaluationQuery>(
  asyncHandler(EvaluationController.getAll)
));

// 获取单个评估
router.get('/:id', createHandler<EvaluationParams, EvaluationResponse>(
  asyncHandler(EvaluationController.getOne)
));

// 创建评估
router.post('/',
  roleAuth(['student', 'teacher', 'admin']),
  validateEvaluation,
  createHandler<EmptyParams, EvaluationResponse, EvaluationBody>(
    asyncHandler(EvaluationController.create)
  )
);

// 更新评估
router.put('/:id',
  roleAuth(['student', 'teacher', 'admin']),
  validateEvaluation,
  createHandler<EvaluationParams, EvaluationResponse, EvaluationUpdateRequest>(
    asyncHandler(EvaluationController.update)
  )
);

// 删除评估
router.delete('/:id',
  roleAuth(['student', 'teacher', 'admin']),
  createHandler<EvaluationParams>(
    asyncHandler(EvaluationController.delete)
  )
);

// 批量删除评估
router.post('/batch-delete',
  roleAuth(['admin']),
  validateBatchDelete,
  createHandler<EmptyParams, void, BatchDeleteBody>(
    asyncHandler(EvaluationController.batchDelete)
  )
);

// 获取评估统计信息
router.get('/stats/summary',
  roleAuth(['teacher', 'admin']),
  createHandler(EvaluationController.getStats)
);

// 提交评估
router.post('/:id/submit',
  roleAuth(['student']),
  createHandler<EvaluationParams, EvaluationResponse, EvaluationStatusUpdate>(
    asyncHandler(async (req, res, next) => {
      req.body = { status: 'submitted' };
      await EvaluationController.updateStatus(req, res, next);
    })
  )
);

// 开始评审
router.post('/:id/start-review',
  roleAuth(['teacher', 'admin']),
  createHandler<EvaluationParams, EvaluationResponse, EvaluationStatusUpdate>(
    asyncHandler(async (req, res, next) => {
      req.body = {
        status: 'reviewing',
        reviewerId: safeObjectId(req.user?._id)
      };
      await EvaluationController.updateStatus(req, res, next);
    })
  )
);

// 完成评审
router.post('/:id/complete-review',
  roleAuth(['teacher', 'admin']),
  validateCompleteReview,
  createHandler<EvaluationParams, EvaluationResponse, EvaluationStatusUpdate>(
    asyncHandler(async (req, res, next) => {
      req.body = {
        status: 'completed',
        reviewerId: safeObjectId(req.user?._id),
        reviewedAt: new Date(),
        feedback: req.body.feedback
      };
      await EvaluationController.updateStatus(req, res, next);
    })
  )
);

// 退回评估
router.post('/:id/reject',
  roleAuth(['teacher', 'admin']),
  createHandler<EvaluationParams, EvaluationResponse, EvaluationStatusUpdate>(
    asyncHandler(async (req, res, next) => {
      req.body = {
        status: 'draft',
        reviewerId: null,
        reviewedAt: null,
        feedback: req.body.feedback
      };
      await EvaluationController.updateStatus(req, res, next);
    })
  )
);

// 导出评估数据
router.get('/export',
  roleAuth(['teacher', 'admin']),
  createHandler(
    asyncHandler(async (_req, res) => {
      // TODO: 实现导出功能
      res.status(501).json({
        success: false,
        message: '功能未实现'
      });
    })
  )
);

export default router;
